package org.jeecg.modules.front.pay;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.AlipayConfig;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.domain.AlipayTradePagePayModel;
import com.alipay.api.request.AlipayTradePagePayRequest;
import com.alipay.api.response.AlipayTradePagePayResponse;
import org.jeecg.common.api.vo.Result;
import org.jeecg.modules.front.util.WechatUtil;
import org.jeecg.modules.front.util.YinShengUtil;
import org.jeecg.modules.yinsheng.YinShengClient;
import org.jeecg.modules.yinsheng.command.base.HeadResponseCommand;
import org.jeecg.modules.yinsheng.command.base.MerchantHeadRequestCommand;
import org.jeecg.modules.yinsheng.command.scancode.FrontCodePayReqCommand;
import org.jeecg.modules.yinsheng.command.scancode.FrontCodePayRespCommand;
import org.jeecg.modules.yinsheng.common.ServiceEnum;
import org.jeecg.modules.yinsheng.common.YsPayConstant;
import org.jeecg.modules.yinsheng.utils.JsonParser;
import org.jeecg.modules.yinsheng.utils.YinshengLogger;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;

@Service
public class AliScan extends Payment {

    public Result<JSONObject> pay(String requestNo, Double amount, Map<String, String> extendParams) throws AlipayApiException {
        // 初始化SDK
        AlipayClient alipayClient = new DefaultAlipayClient(getAlipayConfig());
        // 构造请求参数以调用接口
        AlipayTradePagePayRequest request = new AlipayTradePagePayRequest();
        AlipayTradePagePayModel model = new AlipayTradePagePayModel();
        // 设置商户订单号
        model.setOutTradeNo(requestNo);
        // 设置订单总金额
        model.setTotalAmount(String.valueOf(amount));
        // 设置订单标题
        model.setSubject((extendParams.get("name")+"*"+extendParams.get("num")));
        // 设置产品码
        model.setProductCode("FAST_INSTANT_TRADE_PAY");
        // 设置PC扫码支付的方式
        model.setQrPayMode("1");
        // 设置商户自定义二维码宽度
        model.setQrcodeWidth(100L);
        // 设置订单绝对超时时间
        Date date=new Date();
        SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String time2=sdf.format(new Date(date.getTime()+1800000));
        model.setTimeExpire(time2);
        request.setBizModel(model);
        request.setNotifyUrl(WechatUtil.getNotifyUrl()+"front/order/ali/notify");
        // 第三方代调用模式下请设置app_auth_token
        // request.putOtherTextParam("app_auth_token", "<-- 请填写应用授权令牌 -->");
        AlipayTradePagePayResponse response = alipayClient.pageExecute(request, "POST");
        // 如果需要返回GET请求，请使用
        // AlipayTradePagePayResponse response = alipayClient.pageExecute(request, "GET");
        String pageRedirectionData = response.getBody();
        return Result.ok(pageRedirectionData);
    }

    private static AlipayConfig getAlipayConfig() {
        String privateKey  = "MIIEvgIBADANBgkqhkiG9w0BAQEFAASCBKgwggSkAgEAAoIBAQCU2RRWym4Ld+Mia5xi4xTljfvTKHzF2B4Fh1DlKz8pKUz72JueqkaGJdZic+Aw4fw6jFSNkWfdr7SO9/KEpabrKfiK1mmkCDEtJpvAD4XDP4shrf+XqTJfevyAmTrn5PXL7oESls050zQYGbt7mWyacDw72KTPG9eJlhm5Ndxh08cN1erP7pI5TyReGFlVmBZ2u15606P7NVdBjT9Krde8VlSmteozaV5dvB5QVbU2GVS+NdffoDQKAzve+hMO3H7+yw/k7RDCR8pRrxK9SSmWe66wtlEi1v9CljXIoSPZ7CyvT7e6gPdRQrlms6nkPEya/92fODtg6h2V7RShIC5hAgMBAAECggEAdWqwwq45FM7j29krqk4bw+G3DyFXG/KZ7B6MT9tHnb64vcpnKEiGDXh3n35tEeKSx9S/NlXHq3bZZhZttvm9rLCVIWB2QTE9KbLVFjKogBb2OAE8KHUOCYfI++DkON3Cj7Z7FyDij4TuCTzzSlMC+ukiessxZzc9VYmUjSfRy1C6MjplgAaendW4dxAvHzajxNnNHr0NuzDCBl5EGGvuBfG5f5ZQ8RSnouYkoDz0beGg05+OjMK3itEwRVJiZ0bzcmeppc7TKwgMKzVnKz265RApbW3uz9+yBtq8U/GVdt+HvL7wmEeWJ9NX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RfKjn/CQ6mXuiB10YY6OocfQ5ZZbG3tz9ukUI9XQUVDZR0SegprlucIkEDywMSAwPXhfvCezqLhwEH8N2XckISldZYXiOZObwpIR1cGFCDGfvSzU5T2dyrPUy0/aYGutCkvS/C5dOqGkx3f714jTPRImoZqomWyuKgsXjjhSzlA3YGEic1MdqS6QWPlGwL9qhyHlIAuAvr1xrlfZ1YMxGwwaEzt+9ordNeWbnObVyfwHQQIDAQAB";
        AlipayConfig alipayConfig = new AlipayConfig();
        alipayConfig.setServerUrl("https://openapi.alipay.com/gateway.do");
        alipayConfig.setAppId("2021005129625029");
        alipayConfig.setPrivateKey(privateKey);
        alipayConfig.setFormat("json");
        alipayConfig.setAlipayPublicKey(alipayPublicKey);
        alipayConfig.setCharset("UTF-8");
        alipayConfig.setSignType("RSA2");
        return alipayConfig;
    }

    public Integer payType() {
        return 2;
    }
}
